clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Table 5: Asymmetric pass-through
********************************************************************************

	use "$datafolder\maindata.dta", clear
	
	keep if datem >= 660
	
	global loanChars "maturity_range has_coll_m_any lbalance"
	
	label variable actual_rate "Actual Rate"
	label variable theoretical_rate "Breakeven Rate"
	label variable maturity_range "Maturity"
	label variable has_coll_m_any "Collateral"
	label variable lbalance "Loan Amount"
		
	sort customerid datem
	bysort customerid: gen be_first_x = theoretical_rate if _n==1 & year==2015
	bysort customerid: egen be_first = max(be_first_x)
	
	
	estimates clear
	
	quietly reghdfe actual_rate c.theoretical_rate if be_first < 4.6 , ///
		abs(firmid dateq) cluster(customerid)
		quietly estadd local hasq "Yes"
		quietly estadd local hasind "No"
		quietly estadd local hasfirm "Yes"
		estimates store M1
	
	
	quietly reghdfe actual_rate c.theoretical_rate $loanChars if be_first <4.6, ///
		abs(firmid dateq) cluster(customerid)
		quietly estadd local hasq "Yes"
		quietly estadd local hasind "No"
		quietly estadd local hasfirm "Yes"
		estimates store M2
		
	
	quietly reghdfe actual_rate c.theoretical_rate if be_first >4.6 & be_first!=. , ///
		abs(firmid dateq) cluster(customerid)
		quietly estadd local hasq "Yes"
		quietly estadd local hasind "No"
		quietly estadd local hasfirm "Yes"
		estimates store M3
	
	
	quietly reghdfe actual_rate c.theoretical_rate $loanChars if be_first >4.6 & be_first!=., ///
		abs(firmid dateq) cluster(customerid)
		quietly estadd local hasq "Yes"
		quietly estadd local hasind "No"
		quietly estadd local hasfirm "Yes"
		estimates store M4
	
	
		esttab * using "$outputfolder\table_5.tex", replace  ///
		se star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) r(3) ///
		nonotes r2 label nomtitles noomitted noconstant  ///
		scalars("hasq Quarter FE"  "hasfirm Firm FE") ///
		mgroups("Below-median BE" "Above-median BE", ///
		pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span}))
		
		